// pages/orderInfo/orderInfo.js
var app = getApp();
import { readOrder, weChatPay, delOrder, gettedOrder } from '../../config/https';
import Dialog from '../../vant/dialog/dialog';
import Notify from '../../vant/notify/notify';
import Toast from '../../vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  init(order_id){
    Toast.loading({
      mask: true,
      duration: 0,
      message: '请稍后...'
    });
    readOrder({
      order_id,
      user_id: app.globalData.user_id
    }).then(res => {
      this.setData(res)
      Toast.clear()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init(this.options.order_id)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  // 拨打电话
  PhoneCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.phone_number //仅为示例，并非真实的电话号码
    })
  },

  // 取消订单
  cancelTap() {
    Dialog.confirm({
      message: '确定取消此订单吗？'
    }).then(() => {
      delOrder({
        user_id: app.globalData.user_id,
        order_id: this.data.order_info.id,
        status: this.data.order_info.status,
        type: 1,
      }).then(res => {
        this.succNot("取消订单成功")
        this.init(this.data.order_info.id)
      }).catch(err => {
        this.failNot(err.msg || "取消订单失败")
      })
    }).catch(() => {
      // on cancel
    });
  },

  // 立即支付
  toPay(e) {
    Toast.loading({
      mask: true,
      duration: 0,
      message: '请稍后...'
    });
    const that = this
    weChatPay({
      user_id: app.globalData.user_id,
      order_number: that.data.order_info.order_number,
      total_price: that.data.order_info.realprices
    }).then(res => {
      Toast.clear()
      wx.requestPayment({
        'timeStamp': res.timeStamp,
        'nonceStr': res.nonceStr,
        'package': res.package, //
        'signType': 'MD5',
        'paySign': res.paySign,
        'success': res => {
          Toast.success('支付成功');
          that.init(that.data.order_info.id)
        },
        'fail': err => {
          that.failNot("支付失败,请重新支付")
        }
      })
    })
    // wx.navigateTo({
    //   url: `../payment/payment?data=${JSON.stringify(data)}`
    // })
  },

  // 确认收货
  delivery(e) {
    Dialog.confirm({
      message: '是否确认收货？'
    }).then(() => {
      gettedOrder({
        user_id: app.globalData.user_id,
        order_id: this.data.order_info.id
      }).then(res => {
        this.succNot("确认收货成功")
        this.init(this.data.order_info.id)
      }).catch(err => {
        this.failNot(err.msg || "确认收货失败")
      })
    }).catch(() => {
      // on cancel
    });
  },

  // 立即评价
  estimate(e) {
    wx.navigateTo({
      url: '../evaluate/evaluate',
    })
  },

  // 删除订单
  deleteTap(e) {
    Dialog.confirm({
      message: '是否要删除订单？'
    }).then(() => {
      delOrder({
        user_id: app.globalData.user_id,
        order_id: this.data.order_info.id,
        status: this.data.order_info.status,
        type: 2,
      }).then(res => {
        this.succNot("删除订单成功")
        //获取页面栈
        let pages = getCurrentPages();
        //获取上一个页面
        let prev = pages[pages.length - 2];
        //调用上一个页面的setData方法，从而达到naviagtreBack返回传参的效果
        prev.setData({ refresh: true });
        setTimeout(() => {
          wx.navigateBack()
        }, 1000)
      }).catch(err => {
        this.failNot(err.msg || "删除订单失败")
      })
    }).catch(() => {
      // on cancel
    });
  },

  //申请售后
  toService(e){
    const item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: `../service/service?item=${JSON.stringify(item)}`,
    })
  },

  succNot(msg) {
    Notify({
      text: msg,
      backgroundColor: '#1989fa'
    });
  },

  failNot(msg) {
    Notify({
      text: msg,
      backgroundColor: '#f44'
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})